﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Xml.Linq;

public partial class admin_Media_Video : System.Web.UI.Page
{
  VideoBLL data = new VideoBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        Session["url_back_admin"] = Request.RawUrl;
        GetPositon();
        LoadPosition();
        ShowData();
      }
    }
    public struct Position
    {
      public string Id;
      public string Name;
      public Position(string id, string name)
      {
        Id = id;
        Name = name;
      }
    }
    public static List<Position> listPosition = new List<Position>();
    public void GetPositon()
    {
      if (listPosition != null) listPosition.Clear();
      XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.xml");
      var xPosition = xElement.Descendants("Videos").Descendants("Position");
      foreach (XElement item in xPosition)
      {
        Position position = new Position(item.Attribute("Id").Value, item.Element("Name").Value);
        listPosition.Add(position);
      }
    }
    private void LoadPosition()
    {
      dropPosition.Items.Clear();
      foreach (Position item in listPosition)
      {
        dropPosition.Items.Add(new ListItem(item.Name, item.Id));
      }
      dropPosition.Items.Insert(0, new ListItem("Tất cả", "-1"));
    }
    private void ShowData()
    {
      List<Video> list = data.GetAll();
      if (dropIsEnable.SelectedIndex != 0)
        list = list.Where(p => p.isEnable == Convert.ToBoolean(dropIsEnable.SelectedValue)).ToList();
      if (dropPosition.SelectedIndex != 0)
        list = list.Where(p => p.Position == Convert.ToInt32(dropPosition.SelectedValue)).ToList();

      gridView.DataSource = list;
      gridView.DataBind();
    }
    protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
    {
      lbThongBao.Text = "";
      ShowData();
    }
    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
      lbThongBao.Text = "";
      try
      {
        gridView.PageIndex = e.NewPageIndex;
        ShowData();
      }
      catch { }
    }
    protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
      if ((int)Session["Role"] != 1)
      {
        lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
        return;
      }
      try
      {
        lbThongBao.Text = "";
        int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[1].Text);
        data.Delete(id);
        lbThongBao.Text = "Video đã được xóa";

        ShowData();
      }
      catch (Exception ex)
      {
        lbThongBao.Text = ex.Message;
      }
    }
    private void Enable(bool isEnable)
    {
      foreach (GridViewRow item in gridView.Rows)
      {
        foreach (Control ctrl in item.Cells[0].Controls)
        {
          if (ctrl.GetType() == typeof(CheckBox))
          {
            CheckBox check = (CheckBox)ctrl;
            if (check.Checked)
            {
              int id = Convert.ToInt32(item.Cells[1].Text);
              data.Enable(id, isEnable);
            }
            break;
          }
        }
      }
      ShowData();
    }
    protected void btnVisible_Click(object sender, EventArgs e)
    {
      if ((int)Session["Role"] != 1)
      {
        lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
        return;
      }
      lbThongBao.Text = "";
      Enable(false);
    }
    protected void btnEnable_Click(object sender, EventArgs e)
    {
      if ((int)Session["Role"] != 1)
      {
        lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
        return;
      }
      lbThongBao.Text = "";
      Enable(true);
    }
    protected void btnXoaChon_Click(object sender, EventArgs e)
    {
      if ((int)Session["Role"] != 1)
      {
        lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
        return;
      }
      try
      {
        lbThongBao.Text = "";
        foreach (GridViewRow item in gridView.Rows)
        {
          foreach (Control ctrl in item.Cells[0].Controls)
          {
            if (ctrl.GetType() == typeof(CheckBox))
            {
              CheckBox check = (CheckBox)ctrl;
              if (check.Checked)
              {
                int id = Convert.ToInt32(item.Cells[1].Text);
                data.Delete(id);
              }
              break;
            }
          }
        }
        ShowData();
        lbThongBao.Text = "Video đã được xóa";
      }
      catch (Exception ex)
      {
        lbThongBao.Text = ex.Message;
      }
    }
    protected void linkCheckAll_Click(object sender, EventArgs e)
    {
      lbThongBao.Text = "";
      foreach (GridViewRow item in gridView.Rows)
      {
        foreach (Control ctrl in item.Cells[0].Controls)
        {
          if (ctrl.GetType() == typeof(CheckBox))
          {
            CheckBox check = (CheckBox)ctrl;
            check.Checked = true;
            break;
          }
        }
      }
    }
    protected void linkUnCheckAll_Click(object sender, EventArgs e)
    {
      lbThongBao.Text = "";
      foreach (GridViewRow item in gridView.Rows)
      {
        foreach (Control ctrl in item.Cells[0].Controls)
        {
          if (ctrl.GetType() == typeof(CheckBox))
          {
            CheckBox check = (CheckBox)ctrl;
            check.Checked = false;
            break;
          }
        }
      }
    }
    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      try
      {
        if (e.Row.RowIndex >= 0) //Bo qua dong dau
        {
          int id = Convert.ToInt32(e.Row.Cells[1].Text);          
          bool isEnable = data.GetById(id).isEnable;
          string strPoisiton = e.Row.Cells[3].Text;
          Position position = listPosition.Where(p => p.Id == strPoisiton).FirstOrDefault();
          e.Row.Cells[3].Text = position.Name;
          //Icon check
          Image img = new Image();
          foreach (Control item in e.Row.Cells[5].Controls)
          {
            if (item.GetType() == typeof(Image))
            {
              img = (Image)item;
              break;
            }
          }
          if (isEnable)
            img.ImageUrl = "~/Images/check.png";
          else
          {
            img.ImageUrl = "~/Images/an.png";

          }
        }
      }
      catch { }
    }
    protected void dropPosition_SelectedIndexChanged(object sender, EventArgs e)
    {
      lbThongBao.Text = "";
      ShowData();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
      Response.Redirect("~/admin/Media/Video_Edit.aspx");
    }
}